/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zj.easy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zj.easy.ui.utils.KeyboardUtils;
import com.zj.easy.utils.app.ActivityManager;


/**
 * @author Aidan Follestad (afollestad)
 */
public class DialogBase extends Dialog implements DialogInterface.OnDismissListener, DialogInterface.OnCancelListener, DialogInterface.OnShowListener, DialogInterface.OnKeyListener {

    private OnDismissListener onDismissListener;
    private OnCancelListener onCancelListener;
    private OnShowListener onShowListener;
    private OnKeyListener onKeyListener;

    public DialogBase() {
        this(ActivityManager.getInstance().getCurrentActivity());
    }

    public DialogBase(@NonNull Context context) {
        super(context);
        init(context);
    }


    public DialogBase(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    public DialogBase(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        setOnDismissListener(this);
        setOnCancelListener(this);
        setOnShowListener(this);
        setOnKeyListener(this);
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent ev) {
        KeyboardUtils.dispatchTouchEvent(ev, this);
        return super.onTouchEvent(ev);
    }

    @Override
    public void show() {
        if (DialogManager.getInstance().getCurrentDialog() == this) {
            getWindow().getDecorView().setBackground(null);
            super.show();
        } else {
            DialogManager.getInstance().pushToQueue(this);
        }
    }

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        if (this == listener) {
            super.setOnDismissListener(listener);
        } else {
            onDismissListener = listener;
        }
    }

    @Override
    public void setOnCancelListener(@Nullable OnCancelListener listener) {
        if (this == listener) {
            super.setOnCancelListener(listener);
        } else {
            onCancelListener = listener;
        }
    }

    @Override
    public void setOnShowListener(@Nullable OnShowListener listener) {
        if (this == listener) {
            super.setOnShowListener(listener);
        } else {
            onShowListener = listener;
        }
    }

    @Override
    public void setOnKeyListener(@Nullable OnKeyListener listener) {
        if (this == listener) {
            super.setOnKeyListener(listener);
        } else {
            onKeyListener = listener;
        }
    }

    @Override
    public void dismiss() {
        DialogManager.getInstance().nextTask();
        getWindow().getDecorView().postDelayed(()->{
            super.dismiss();
        },100);
    }

    @Override
    public void cancel() {
        super.cancel();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (onDismissListener != null) {
            onDismissListener.onDismiss(dialog);
        }
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (onCancelListener != null) {
            onCancelListener.onCancel(dialog);
        }
    }

    @Override
    public void onShow(DialogInterface dialog) {
        if (onShowListener != null) {
            onShowListener.onShow(dialog);
        }
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (onKeyListener != null) {
            return onKeyListener.onKey(dialog, keyCode, event);
        }
        return false;
    }
}
